<?php
/**
 * Created by PhpStorm.
 * User: 72925
 * Date: 2018/5/10
 * Time: 9:26
 * order: 文件上传
 */

namespace app\index\controller;

use think\Controller;
use app\index\model\Contact;
use app\index\model\Advert;
use app\index\model\User;
use app\index\model\Fbtask;
use app\index\model\Task;
use think\File;

class Uploads extends Base
{
	//上传图片
 	public function uploadpic()
 	{	
 		$id= input('id');
        $status = $this->status;
         if($status == "error"){
                        $dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
                        return json_encode($dat);
                }
                else
                {					

                	
						$img= $_FILES['fileUrl'];
					    $filetype = ['image/jpg', 'image/jpeg', 'image/gif', 'image/bmp', 'image/png'];	
				 		$file['type'] = $img['type'];
					    $file['size'] = $img['size'];
					    $size = 5242880;
					    if(!in_array($file['type'], $filetype))
					   	{	
					   		$dat = ['code'=>400,'msg'=>'图片类型错误'];
							echo json_encode($dat);

					   	}
					   	elseif($file['size']>$size)
					   	{
					   		$dat = ['code'=>401,'msg'=>'图片大小超过限制'];
							echo json_encode($dat);
					   	}
					   	else
					   	{	

					    	$file['name'] = md5(time().mt_rand(0,1000)).$img['name'];
							$path = ROOT_PATH.'public/uploads/';
					   		$new = str_replace('\\', '/', $path);
					   		$true = move_uploaded_file($img['tmp_name'],$new.$file['name']);
					   		if($true)
					   		{
					   			$filepath = "https://".$_SERVER['HTTP_HOST'].'/public/uploads/';
					   			$newimg = $filepath.$file['name'];
								$dat = ['code'=>200,'msg'=>'上传成功','img'=>$newimg];
								echo json_encode($dat,TRUE);
					   		}
					   		else
					   		{
					   			echo "err";
					   		}
					   	}
				}
     }
}